package com.poker.duiued;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.poker.iuious.yhyhjs.jklimj.User;
import com.poker.jkiuus.R;
import com.poker.jkiuus.uyrfgd.AppManager;
import com.poker.jkiuus.uyrfgd.DatabaseHelper;
import com.poker.jkiuus.uyrfgd.MD5Utils;


public class UserAccount_PwsActivity extends AppCompatActivity {
    private String userName;
    private DatabaseHelper databaseHelper;
    private User user = new User();
    private EditText et_old_password;
    private EditText et_new_password;
    private EditText et_confirm_new_password;
    private Button btn_change_password;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.change_password_layout);

        databaseHelper = new DatabaseHelper(this);

        if (Build.VERSION.SDK_INT >= 33) {
            user = getIntent().getParcelableExtra("USER",User.class);
        }else {
            userName = getIntent().getStringExtra("USER_NAME");
            user = databaseHelper.queryUserByNameOrEmail(userName);
        }

        AppManager.addActivity(this);

        initView();
    }

    private void initView(){
        et_old_password = findViewById(R.id.et_old_password);
        et_new_password = findViewById(R.id.et_new_password);
        et_confirm_new_password = findViewById(R.id.et_confirm_new_password);
        btn_change_password = findViewById(R.id.btn_change_password);

        btn_change_password.setOnClickListener(v -> changePws());

        et_confirm_new_password.setOnFocusChangeListener((v, hasFocus) -> {
            if (!hasFocus) {
                if (!isValidConfirmPws(et_confirm_new_password.getText().toString())) {
                    et_confirm_new_password.setError("The new password is not the same as the confirmation password!");
                }
            }
        });
    }

    private void changePws(){
        String old_password = et_old_password.getText().toString().trim();
        String new_password = et_new_password.getText().toString().trim();
        String confirm_new_password = et_confirm_new_password.getText().toString().trim();

        if (old_password.length() <= 0 || new_password.length() <= 0 || confirm_new_password.length() <= 0){
            showChangeDialog("Please don't leave it blank!",false);
            et_old_password.setError("Please don't leave it blank!");
            et_new_password.setError("Please don't leave it blank!");
        }else if(!new_password.equals(confirm_new_password)){
            showChangeDialog("The new password is not the same as the confirmation password!",false);
            et_confirm_new_password.setError("The new password is not the same as the confirmation password!");
        }else if(!MD5Utils.passwordIsTrue(old_password,user.getPassword())){
            showChangeDialog("The old password is incorrect!",false);
            et_old_password.setError("The old password is incorrect!");
        }else {
            new_password = MD5Utils.string2MD5(new_password);
            boolean b = databaseHelper.updateDataPws(new User(user.getName(), new_password));
            if (b){
                showChangeDialog("Modification successful!",true);
            }else {
                showChangeDialog("Failed to modify!Please try again later!",false);
            }
        }
    }

    private void showChangeDialog(String message,boolean flag) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("PROMPT");
        builder.setMessage(message);
        builder.setPositiveButton("OK", (dialog, which) -> {
            if (flag) {
                Intent intent = new Intent(UserAccount_PwsActivity.this,LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                intent.putExtra("USER_NAME", userName);
                startActivity(intent);
                finish();
                AppManager.finishAll();
                dialog.dismiss();
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    private boolean isValidConfirmPws(String confirmPws) {
        return confirmPws.equals(et_confirm_new_password.getText().toString().trim());
    }

}
